//
//  DanPinViewController.m
//  PhoenixNews
//
//  Created by dllo on 16/1/25.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "DanPinViewController.h"
#import "PhoneixNews.pch"
#import "DanPinCollectionViewCell.h"
@interface DanPinViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) UICollectionView *collection;
@property (nonatomic, retain) NSMutableArray *mutableArray;// 单品内容数组
@end

@implementation DanPinViewController
- (void)dealloc {
    [_collection release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 每个collection大小
    layout.itemSize = CGSizeMake(500 / 3, 220);
    layout.minimumInteritemSpacing = 20;
    layout.minimumLineSpacing = 10;
    // collection 在试图的大小
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 20, self.view.frame.size.width - 20, self.view.frame.size.height  - 190) collectionViewLayout:layout];
    self.collection.backgroundColor = [UIColor whiteColor];
    
    [self.collection registerClass:[DanPinCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    // 隐藏滚动条
    [self.collection setShowsVerticalScrollIndicator:NO];
    [_collection release];
    [self.view addSubview:_collection];
    [self getData];
}
#pragma mark -数据解析
- (void)getData {
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    [sessionManager GET:DanPin parameters:self progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject[@"data"];
       
        NSArray *array = dic[@"items"];
        self.mutableArray = [HeadlineModel modelHanderWithArray:array];
        
        [self.collection reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}


#pragma mark -CollectionView协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _mutableArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    DanPinCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    HeadlineModel *headline = _mutableArray[indexPath.row];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:headline.cover_image_url]];
    cell.name.text = headline.name;
    cell.price.text = headline.price;
    cell.likes.text = [NSString stringWithFormat:@"%@k", headline.favorites_count];
    return cell;
}
#pragma mark - Cell点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    DetailePageViewController *detaile = [[DetailePageViewController alloc] init];
    HeadlineModel *headline = _mutableArray[indexPath.row];
    detaile.urlStr = headline.url;
    [self.navigationController pushViewController:detaile animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
